//
//  HomeGameViewCell.swift
//  d11
//
//  Created by peale on 2018/11/19.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class HomeGameViewCell: UITableViewCell {
    var bgImgView: UIImageView?
    var bottomView:UIImageView?
    var topImgView: UIImageView?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.clear
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        self.bottomView = UIImageView.init()
        self.bottomView?.image = imageNamed("icon_home_game_bottom")
        self.contentView.addSubview(self.bottomView!)
        self.bottomView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-10)
            make.height.equalTo(18)
            make.left.equalToSuperview().offset(5)
            // make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
     
        self.bgImgView = UIImageView.init()
        self.contentView.addSubview(self.bgImgView!)
        self.bgImgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        self.topImgView = UIImageView.init()
        self.topImgView?.image = imageNamed("icon_home_whz")
        self.contentView.addSubview(self.topImgView!)
        self.topImgView?.snp.makeConstraints{(make) in
            make.width.equalTo(72)
            make.height.equalTo(60)
            make.right.equalToSuperview().offset(-3)
            // make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(1)
        }
     
    }
    
    func setData(imgUrl:String,isBottom:Bool,gameId:String,gameCount:Int,isShow:Bool) -> Void {
//        if isBottom == true {
//             self.bottomView?.isHidden = false
//            self.bgImgView?.snp.updateConstraints({ (make) in
//                make.height.equalToSuperview().offset(-10)
//            })
//        }else{
            self.bottomView?.isHidden = true
            self.bgImgView?.snp.updateConstraints({ (make) in
                make.height.equalToSuperview()
            })
 //       }
        
        let hallDict = getHall(withId: gameId)
        let status = hallDict?["status"] as? Int ?? -1
        self.topImgView?.isHidden = true
        if status == 0{
            self.topImgView?.isHidden = false
        }
        
        if kIsIpad == true{
//            if isShow == true{
//                self.bgImgView?.image = UIImage.init(named: "icon_home_test_img_ipad")
//            }else{
                if gameCount == 1{
                    self.bgImgView?.sd_setImage(with: URL(string: imgUrl), placeholderImage: imageNamed("icon_newhome_game_zwt_bg_one_ipad"), options: .refreshCached, completed: nil)
                }else if gameCount == 2{
                    self.bgImgView?.sd_setImage(with: URL(string: imgUrl), placeholderImage: imageNamed("icon_newhome_game_zwt_bg_max_ipad"), options: .refreshCached, completed: nil)
                } else
                {
                    self.bgImgView?.sd_setImage(with: URL(string: imgUrl), placeholderImage: imageNamed("icon_newhome_game_zwt_bg_ipad"), options: .refreshCached, completed: nil)
                }
    //        }
           
        }else{
//            if isShow == true{
//                self.bgImgView?.image = UIImage.init(named: "icon_home_test_img")
//            }else{
                if gameCount == 1{
                    self.bgImgView?.sd_setImage(with: URL(string: imgUrl), placeholderImage: imageNamed("icon_newhome_game_zwt_bg_one"), options: .refreshCached, completed: nil)
                }else if gameCount == 2{
                    self.bgImgView?.sd_setImage(with: URL(string: imgUrl), placeholderImage: imageNamed("icon_newhome_game_zwt_bg_max"), options: .refreshCached, completed: nil)
                } else
                {
                    self.bgImgView?.sd_setImage(with: URL(string: imgUrl), placeholderImage: imageNamed("icon_newhome_game_zwt_bg"), options: .refreshCached, completed: nil)
                }
//            }
            
        }
       
      
    }
    
    func getHall(withId: String?) -> [String:Any]? {
        if let hallInfoData = GlobalInfo.hallInfoData {
            for hall in hallInfoData {
                let id = hall["id"] as? String ?? "0"
                if id == withId {
                    return hall
                }
            }
        }
        return nil
    }
}
